package com.whty.ws_fengx.controller;

import java.util.Date;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.whty.ws_fengx.common.Code;
import com.whty.ws_fengx.common.Msg;
import com.whty.ws_fengx.common.Result;
import com.whty.ws_fengx.domain.User;
import com.whty.ws_fengx.service.UserService;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.whty.ws_fengx.constant.UserConstant.USER_LOGIN_STATUS;

@Api(tags = {"用户相关接口"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 0)
    @ApiResponses({
            @ApiResponse(code = 20061, message = "用户注册成功"),
            @ApiResponse(code = 20060, message = "用户注册失败")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userAccount", value = "用户账户", required = true, paramType = "path"),
            @ApiImplicitParam(name = "userPassword", value = "用户密码", required = true, paramType = "path"),
            @ApiImplicitParam(name = "checkPassword", value = "用户再次输入密码", required = true, paramType = "path")
    })
    @PostMapping("/register")
    public Result<Boolean> userRegister(@RequestParam("username") String userAccount,
                                        @RequestParam("password") String userPassword,
                                        @RequestParam("checkpassword") String checkPassword) {
        boolean flag = userService.userRegister(userAccount, userPassword, checkPassword);
        return flag == true ?
                Result.buildSuccessMessage(true, Msg.REGISTIER_OK) : Result.buildFailedMessage(Code.REGISTER_ERR, Msg.REGISTIER_ERR);
    }


    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 10)
    @ApiResponses({
            @ApiResponse(code = 20051, message = "用户登录成功"),
            @ApiResponse(code = 20050, message = "用户登录失败")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户账号", required = true, paramType = "path"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, paramType = "path")
    })
    @GetMapping("/login")
    public Result<User> userLogin(@RequestParam("username") String userAccount,
                                  @RequestParam("password") String userPassword,
                                  HttpServletRequest request) {
        User user = userService.userLogin(userAccount, userPassword, request);
        return user != null ?
                Result.buildSuccessMessage(user, Msg.LOGIN_OK) : Result.buildFailedMessage(Code.LOGIN_ERR, Msg.LOGIN_ERR);
    }

    @ApiOperation("根据用户账号查找用户信息")
    @ApiOperationSupport(order = 11)
    @ApiResponses({
            @ApiResponse(code = 20041, message = "用户登录成功"),
            @ApiResponse(code = 20040, message = "用户登录失败")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userAccount", value = "用户账号", required = true)
    })
    @GetMapping("/{userAccount}")
    public Result<User> getUserByAccount(@PathVariable String userAccount) {
        User user = userService.getUserByAccount(userAccount);
        return user != null ?
                Result.buildSuccessMessage(user) : Result.buildFailedMessage(Code.SYSTEM_ERR, Msg.USER_LOST);
    }

    //todo 修改用户密码


    //todo 添加好友
    @GetMapping("/{friendAccount}")
    public Result<Boolean> userAddFriend(@PathVariable String friendAccount, HttpServletRequest request) {
        User friend = userService.getUserByAccount(friendAccount);
        if (friend == null) {
            return new Result<>(Code.GET_ERR, false, Msg.USER_LOST);
        }
        Object o = request.getSession().getAttribute(USER_LOGIN_STATUS);
        User I = (User) o;
        if (I == null) {
            return new Result<>(Code.GET_ERR, false, Msg.ERR);
        }
        //添加好友（好友在前）
        boolean flag = userService.addUserFriend(friend, I);
        return flag == true ?
                Result.buildSuccessMessage(true, Msg.ADD_FRIEND_OK) : Result.buildFailedMessage(Code.SYSTEM_ERR, Msg.ERR);
    }


    //todo 查看好友列表
    //todo 删除好友
    //todo 修改好友备注
    //todo 添加群
    //todo 退出群
    //todo 删除群
    //todo 修改群信息


    @ApiOperation("用户退出登录")
    @ApiOperationSupport(order = 20)
    @ApiResponses({
            @ApiResponse(code = 20071, message = "用户登录成功"),
            @ApiResponse(code = 20070, message = "用户登录失败")
    })
    @GetMapping("/logout")
    public Result<Boolean> userLogout() {
        return null;
    }


}
